package com.drzhang.movieticketpicker.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by drzhang on 2017/11/10.
 */

public class AudioPlayerView extends View {

    private int duration;
    private Paint mCirclePaint;
    private Paint mTextPaint;
    private Paint mCurvPaint;
    private Path mCurvPath;

    private int mRadius;
    private Timer mTimer;
    private TimerTask mTask;
    private Handler mHandler;
    public static final int INVALIDATE = 100;

    public AudioPlayerView(Context context) {
        super(context);
        initView();
    }

    public AudioPlayerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public AudioPlayerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView(){
        mCirclePaint = new Paint();
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setDither(true);
        mCirclePaint.setStyle(Paint.Style.STROKE);
        mCirclePaint.setStrokeWidth(4);
        mCirclePaint.setColor(Color.BLUE);

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setDither(true);
        mTextPaint.setStyle(Paint.Style.STROKE);
        mTextPaint.setTextSize(48);
        mTextPaint.setColor(Color.RED);

        mCurvPaint = new Paint();
        mCurvPaint.setAntiAlias(true);
        mCurvPaint.setDither(true);
        mCurvPaint.setStyle(Paint.Style.FILL);
        mCurvPaint.setColor(Color.GREEN);

        mCurvPath = new Path();

        mTimer = new Timer();
        mTask = new UpdateTask();
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == INVALIDATE){
                    if (duration == 0){
                        mTimer.cancel();
                        return true;
                    }
                    duration -- ;
                    invalidate();
                    return true;
                }
                return false;
            }
        });

        start();

    }

    public void setDuration(int duration){
        this.duration = duration;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        mRadius = Math.min(width,height)/2;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {

        String content = duration + "S";

        // 1 画外圈的圆
        canvas.drawCircle(mRadius,mRadius,mRadius,mCirclePaint);

        Rect textRect = new Rect();
        mTextPaint.getTextBounds(content,0,content.length(),textRect);

        //2 画时间显示文字
        canvas.drawText(content,mRadius - textRect.width()/2,mRadius/2,mTextPaint);

        //3 画波浪
        mCurvPath.moveTo(0,mRadius);
        mCurvPath.quadTo(mRadius/2,0,mRadius,mRadius);
        mCurvPath.quadTo(mRadius*3/2,mRadius*3/2,mRadius*2,mRadius);
        mCurvPath.arcTo(0,0,2*mRadius,2*mRadius,0,180,false);
        canvas.drawPath(mCurvPath,mCurvPaint);
    }

    private void start(){
        mTimer.scheduleAtFixedRate(mTask,1000,1000);
    }

    class UpdateTask extends TimerTask{

        @Override
        public void run() {
            mHandler.sendEmptyMessage(INVALIDATE);
        }
    }


}
